home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: INITIALIZATION of a MEMBER CLASS of a CLASS
- Date: Thu, 18 Apr 1996 11:51:21 -0400
- Organization: Datalytics, Inc
- Message-ID: <317664F9.1E16@datalytics.com>
- References: <4l37o3$iug@risky.ecs.umass.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Kiran K Toutireddy wrote:
- >
- > Hi,
- > I have a question related to initializing the member
- > classes inside a class data structure. The following example
- > should explain the question.
- >
- > I have a class called Engine, which can be initialized to Gas
- > engine,Deisel Engine, Electric Engine, etc. by default, it is
- > electric engine.(default constructo
- >
- > I have a totally unrelated class(no inheritances...) called
- > Car.
- >
- > Now this Car class has a member
- >
- > Engine car_engine;
- >
- > Since all the cars I consider have only gas engines(
- > no EVs considered :-),
- > I want to initialize the car_engine member to be Gas
- > engine by default, instead of the default electric engine. How
- > do I do that?
- > the following is some pseudo code of the above example
- >
- > class Engine{
- > nuts..bolts..pipes..rods...;
- > public:
- > Engine(){ make this an electric engine,....;}
- > Engine(int Gas){ make it a gas engine..}
-
- Since you can't have negative cylinders, wouldn't size_t be a
- better type than int?
-
- > Engine(float Deisel){ make it a deisel engine;}
-
- float seems an even worse type for specifying the number of
- cylinders.
-
- > };
- >
-
- How about this instead?
-
- class Engine
- {
- public:
- enum engine_type
- {
- ELECTRIC,
- GASOLINE,
- DIESEL
- };
-
- Engine(
- engine_type i_Type = ELECTRIC,
- size_t i_Cylinders = 0);
- private:
- engine_type m_Type;
- size_t m_Cylinders;
- etc.
- };
-
- You could overload the ctor to avoid specifying cylinders for an
- electric engine; maybe you want to specify the battery voltage?
-
- >
- > class Car{
- > Engine car_engine;
- > public:
- > Car(){ how do i initialize the car_engine to be of type Gas here?;}
- > };
- >
- > I tried initializing the car_engine by saying Engine
- > car_engine(int Gas), but it gives a warning saying that ANSI
- > c++ forbids such declarations.
- >
- > Can anybody suggest a better way of doing this?
- > thanks in advance
- > -kiran
-
- How about this:
-
- class Car
- {
- public:
- Car(
- const size_t i_Cylinders):
- engine(i_Cylinders)
- {
- };
- private:
- Engine engine;
- };
-
- I've assumed that you would want to specify the number of
- cylinders in Car's ctor.
-
- The solution is to initialize engine in the initializer list.
- (If you change Engine as I suggested above, the initializer
- would be this:
-
- engine(
- Engine::GASOLINE,
- i_Cylinders)
- )
-
- Of course, Engine could provide another mf for changing the
- engine type after construction (which would be necessary if you
- wanted an array of gasoline engines). In that case, you could
- leave the default initialization of engine and call the other mf
- to change it to gasoline.
-
- The latter approach is not as useful (except for arrays) because
- of the work involved in invoking the default ctor then changing
- the engine type. The more a class does in the default ctor and
- undoes in the other mf, the more useless work you cause your
- code to perform.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-